package com.atguigu.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.atguigu.common.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author tjf
 * @since 2022-01-03
 */
@RestController
@Api(tags = "会员账户")
@RequestMapping("/api/core/userAccount")
@Slf4j
public class UserAccountController {
    private final UserAccountService userAccountService;

    public UserAccountController(UserAccountService userAccountService) {
        this.userAccountService = userAccountService;
    }

    @ApiOperation("充值")
    @PostMapping("/auth/commitCharge/{chargeAmt}")
    public R commitCharge(@ApiParam(value = "充值金额", required = true) @PathVariable BigDecimal chargeAmt, @RequestHeader String token) {
        Long userId = JwtUtils.getUserId(token);
        String formStr = userAccountService.commitCharge(chargeAmt, userId);
        return R.ok().data("formStr", formStr);
    }

    @ApiOperation("用户充值异步回调接口")
    @PostMapping("/notify")
    public String notify(HttpServletRequest httpServletRequest) {
        // 获取汇付宝回调时携带的参数
        Map<String, Object> map = RequestHelper.switchMap(httpServletRequest.getParameterMap());
        log.info("用户充值异步回调接收参数如下：" + JSON.toJSONString(map));

        // 校验签名
        if (!RequestHelper.isSignEquals(map)) {
            log.error("用户账户绑定异步回调签名验证错误：" + JSON.toJSONString(map));
            return "fail";
        }

        if ("0001".equals(map.get("resultCode"))) {
            log.info("验证签名成功，开始充值账户");

            return userAccountService.notify(map);

        }
        return "success";
    }

    @ApiOperation("查询账户余额")
    @GetMapping("/auth/getAccount")
    public R getAccount(@RequestHeader String token) {
        Long userId = JwtUtils.getUserId(token);
        BigDecimal account = userAccountService.getAccount(userId);
        return R.ok().data("account", account);
    }

    @ApiOperation("用户提现")
    @PostMapping("/auth/commitWithdraw/{fetchAmt}")
    public R commitWithdraw(@ApiParam(value = "提现金额", required = true) @PathVariable BigDecimal fetchAmt, @RequestHeader String token) {
        Long userId = JwtUtils.getUserId(token);
        String formStr = userAccountService.commitWithdraw(fetchAmt, userId);
        return R.ok().data("formStr", formStr);
    }

    @ApiOperation("用户提现异步回调接口")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest httpServletRequest) {
        // 获取汇付宝回调时携带的参数
        Map<String, Object> map = RequestHelper.switchMap(httpServletRequest.getParameterMap());
        log.info("用户提现异步回调接收参数如下：" + JSON.toJSONString(map));

        // 校验签名
        if (!RequestHelper.isSignEquals(map)) {
            log.error("用户账户绑定异步回调签名验证错误：" + JSON.toJSONString(map));
            return "fail";
        }

        if ("0001".equals(map.get("resultCode"))) {
            log.info("验证签名成功，开始提现");

            return userAccountService.notifyWithdraw(map);

        }
        return "success";
    }
}

